package cn.hdustea.shopNavigation.ui.view.main.beauty

import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.widget.AppCompatImageView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.config.ConstantUrl
import cn.hdustea.shopNavigation.entity.BannerEntity
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.activity_beauty.*

class BeautyActivity : AppCompatActivity() {
    private val tabs = arrayOf("发现", "美发", "美甲美瞳",
            "美容SPA", "医学美容", "美妆",
            "养发", "瘦身纤体", "瑜伽",
            "舞蹈", "纹眉纹绣", "纹身",
            "祛痘", "产后塑形")

    companion object {
        fun startActivity(context: Context) {
            context.startActivity(Intent(context, BeautyActivity::class.java))
        }
    }

    private val tabFragmentList = arrayListOf<Fragment>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_beauty)
        initView()
        initBanner()
    }





    private fun initView() {
        for (tab in tabs) {
            tabFragmentList.add(BeautyFragment.newInstance(tab))
        }
        beauty_view_pager.currentItem = 0
        beauty_view_pager.adapter = object : FragmentPagerAdapter(supportFragmentManager,
                BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {
            override fun getCount(): Int = tabFragmentList.size

            override fun getItem(position: Int): Fragment = tabFragmentList[position]

            override fun getPageTitle(position: Int): CharSequence? = tabs[position]
        }
        beauty_view_pager.offscreenPageLimit = 3
        beauty_tab_layout.setupWithViewPager(beauty_view_pager, false)



        beauty_search_view.setOnClickListener {

        }
    }

    private fun initBanner() {
        val banners = mutableListOf<BannerEntity>()
        for (i in 1..3) {
            val banner = BannerEntity(id = i.toString(),
                    bannerPath = "${ConstantUrl.BASE_URL}/image/banner/beauty$i.jpg",
                    url = null)
            banners.add(banner)
        }
        im_banner.setBannerData(banners)
        im_banner.loadImage { _, model, view, _ ->
            Glide.with(this)
                    .load((model as BannerEntity).bannerPath)
                    .into(view as AppCompatImageView)
        }
    }
}